<template>
  <div>
    <Temp title="地下模式">
      <div class="underground">
        <div>
          <label class="title">地表透明度：</label>
          <el-slider
            :format-tooltip="formatTooltip"
            @change="onAlphaChange"
            :max="1"
            :min="0"
            :step="0.1"
            v-model="alpha"
          />
        </div>
      </div>
    </Temp>
  </div>
</template>

<script>
import Temp from '@/components/utool/components/temp'
import { getAction } from '@/utils/helper'

let mapViewer = null

export default {
  inject: ['viewerId'],
  components: {
    Temp
  },
  data() {
    return {
      alpha: 1,
      layers: []
    }
  },
  async beforeMount() {
    const { viewer } = await getAction(this.viewerId)
    mapViewer = viewer
    this.alpha = mapViewer.scene.globe.baseColor.alpha
    mapViewer.scene.screenSpaceCameraController.enableCollisionDetection =
      this.alpha === 1
    mapViewer.scene.screenSpaceCameraController.minimumZoomDistance =
      this.alpha === 1 ? 1 : -1000
    mapViewer.scene.globe.globeAlpha = this.alpha
    mapViewer.scene.undergroundMode = this.alpha !== 1
    this.layers = mapViewer.scene.imageryLayers._layers
    if (this.layers && this.layers.length > 0) {
      this.layers.forEach((layer) => {
        layer.alpha = this.alpha
      })
    }
  },
  beforeDestroy() {
    if (mapViewer) {
      mapViewer.scene.screenSpaceCameraController.enableCollisionDetection = true
      mapViewer.scene.screenSpaceCameraController.minimumZoomDistance = 1
      mapViewer.scene.globe.globeAlpha = 1
      mapViewer.scene.undergroundMode = false
      mapViewer.scene.globe.baseColor.alpha = 1
      mapViewer.scene.terrainProvider.isCreateSkirt = false
      if (this.layers && this.layers.length > 0) {
        this.layers.forEach((layer) => {
          layer.alpha = 1
        })
      }
    }
  },
  methods: {
    onAlphaChange(value) {
      this.alpha = value
      if (mapViewer) {
        mapViewer.scene.screenSpaceCameraController.enableCollisionDetection =
          value === 1
        mapViewer.scene.screenSpaceCameraController.minimumZoomDistance =
          value === 1 ? 1 : -1000
        mapViewer.scene.globe.globeAlpha = value
        mapViewer.scene.undergroundMode = value !== 1
        mapViewer.scene.globe.baseColor.alpha = value
        mapViewer.scene.terrainProvider.isCreateSkirt = false
        if (this.layers && this.layers.length > 0) {
          this.layers.forEach((layer) => {
            layer.alpha = this.alpha
          })
        }
      }
    },
    formatTooltip(value) {
      return `${value}`
    }
  }
}
</script>

<style lang="scss" scoped>
.underground {
  min-width: 290px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  background-color: #193b51;
  border-radius: 0 0 8px 8px;

  div {
    width: 100%;
    display: flex;
    align-items: center;

    .el-slider {
      flex: 1;
    }
  }
}

.title {
  width: 100px;
  font-size: 14px;
  color: #fefefe;
}
</style>
